package com.hualan.dao.impl;

import com.hualan.bean.PanghuCommunity;
import com.hualan.bean.PanghuCourierStation;
import com.hualan.dao.StationDao;
import com.hualan.utils.C3p0Util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class StationDaoImpl implements StationDao {

    //获取数据总条数
    @Override
    public Long StationIdCount() {
        String sql = "select count(*) as count from panghu_courier_station ";
        Map<String, Object> map = C3p0Util.queryOne(sql);
        Long count = (Long) map.get("count");
        return count;
    }


    //搜索全部的信息,按照id降序
    @Override
    public List<PanghuCourierStation> selectAllCourierStation(int page, int size) {
        String sql = "select * from panghu_courier_station order by courier_station_id desc limit ?,?";
        List<PanghuCourierStation> list = new ArrayList<>();
        list = C3p0Util.query(sql, PanghuCourierStation.class, (page - 1) * size, size);
        for (PanghuCourierStation p : list) {
            PanghuCommunity panghuCommunity = selectCommunityId(p.getCourierStationCommunityId());
            p.setCourierStationCommunityName(panghuCommunity.getCommunityName());
            p.setCourierStationOrderCount(0);
        }
        return list;
    }

    //增加一个数据
    @Override
    public int insert(PanghuCourierStation station) {
        int row = C3p0Util.update("insert into panghu_courier_station (courier_station_name,courier_station_image,courier_station_province,courier_station_city,courier_station_district,courier_station_address,courier_station_community_id)  values (?,?,?,?,?,?,?)", station.getCourierStationName(), station.getCourierStationImage(), station.getCourierStationProvince(), station.getCourierStationCity(), station.getCourierStationDistrict(), station.getCourierStationAddress(), station.getCourierStationCommunityId());
        return row;
    }

    //改变驿站的状态(上架,下架)
    @Override
    public int updateState(int id, int status) {
        int row = 0;
        String sql = "update panghu_courier_station set courier_station_status=? where courier_station_id=?";
        row = C3p0Util.update(sql, status, id);
        return row;
    }

    //根据id修改信息
    @Override
    public int updateListState(PanghuCourierStation station) {
        String sql = "update panghu_courier_station set courier_station_name=?,courier_station_image=?,courier_station_province=?,courier_station_city=?,courier_station_district=?,courier_station_address=?,courier_station_community_id=?  where courier_station_id=?";
        int row = C3p0Util.update(sql, station.getCourierStationName(), station.getCourierStationImage(), station.getCourierStationProvince(), station.getCourierStationCity(), station.getCourierStationDistrict(), station.getCourierStationAddress(), station.getCourierStationCommunityId(), station.getCourierStationId());
        return row;
    }

    //根据小区的id查询小区的数据
    @Override
    public PanghuCommunity selectCommunityId(int id) {
        String sql = "select *from panghu_community where community_id=?";
        PanghuCommunity panghuCommunity = C3p0Util.queryOne(sql, PanghuCommunity.class, id);
        return panghuCommunity;
    }

    //获取小区的全部全部信息
    @Override
    public List<PanghuCommunity> communityNameArr() {
        return C3p0Util.query("select*from panghu_community", PanghuCommunity.class);
    }

    //
    @Override
    public List<PanghuCourierStation> courierStationSelect(String name, String startTime, String endTime, int pageNum, int pageSize, String communityName) {
        String sql = "select*from panghu_courier_station WHERE 1=1";
        List<PanghuCourierStation> list=new ArrayList<>();
        List<Object> params = new ArrayList<>();
        if (name != null && !"".equals(name)) {
            sql += " and courier_station_name like ?";
            params.add("%"+name+"%");
        }

        if (communityName != null && !"".equals(communityName)) {
            int id=-1;
            String str="";
            List<PanghuCommunity> communities=this.communityNameArr();
            for (PanghuCommunity p : communities) {
                if (p.getCommunityName().contains(communityName)){
                    id=p.getCommunityId();
                    str+=id+",";
                };
            }
            if ("".equals(str)){
                sql+=" AND courier_station_community_id=-1";
            }else {
                str=str.substring(0,str.length()-1);
                sql += " AND courier_station_community_id in"+"("+str+")";
            }
        }
        if (startTime != null && !"".equals(startTime)) {
            sql += " AND  add_time>=?";  //select * from panghu_community where 1=1 AND  create_time>=xxxx
            params.add(startTime);
        }
        if (endTime != null && !"".equals(endTime)) {
            sql += " AND  add_time<=?";//select * from panghu_community where 1=1 AND  create_time<=xxxx
            params.add(endTime);
        }
        if (pageNum==0){
            sql += " order by courier_station_id desc ";
        }else {
            sql += " order by courier_station_id desc  limit " + (pageNum-1)*pageSize + "," + pageSize;
        }

        System.out.println(sql);
        list=C3p0Util.query(sql, PanghuCourierStation.class,params.toArray(new Object[0]));
	    System.out.println(list);
        for (PanghuCourierStation p : list) {
	        PanghuCommunity panghuCommunity = selectCommunityId(p.getCourierStationCommunityId());
	        System.out.println(panghuCommunity);
	        if (panghuCommunity!=null){
		        p.setCourierStationCommunityName(panghuCommunity.getCommunityName());
	        }
	        p.setCourierStationOrderCount(0);
        }
        return list;
    }

    public static void main(String[] args) {
//        System.out.println(new StationDaoImpl().StationIdCount());
//        System.out.println(new StationDaoImpl().selectAllCourierStation(1, 5));
//        PanghuCourierStation panghuCourierStation=new PanghuCourierStation();
//        panghuCourierStation.setCourierStationName(" ");
//        panghuCourierStation.setCourierStationProvince("");
//        panghuCourierStation.setCourierStationCity(" ");
//        panghuCourierStation.setCourierStationDistrict("");
//        panghuCourierStation.setCourierStationAddress("");
//        panghuCourierStation.setCourierStationImage("");
//        panghuCourierStation.setCourierStationCommunityId(2);
//        System.out.println(new StationDaoImpl().insert(panghuCourierStation));
//
        System.out.println(new StationDaoImpl().courierStationSelect("大",null,null,1,5,""));
    }
}
